package com.sky.aion.utils;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.sky.aion.AionConstant;
import com.sky.aion.redis.JedisUtils;
import redis.clients.jedis.Jedis;

public class SpeakUtils {
    private static SpeakUtils sp;
    public static SpeakUtils getInstance(){
        if (sp==null){
            return new SpeakUtils();
        }
        return sp;
    }
    private Dispatch sapo;
    private ActiveXComponent sap;
    private SpeakUtils() {
        // ？？ 这个Sapi.SpVoice是需要安装什么东西吗，感觉平白无故就来了
        sap = new ActiveXComponent("Sapi.SpVoice");
        // Dispatch是做什么的？
        sapo = sap.getObject();

    }

    public  void speak(String str){
        try {
            // 音量 0-100
            sap.setProperty("Volume", new Variant(100));
            // 语音朗读速度 -10 到 +10
            sap.setProperty("Rate", new Variant(-2));
            Variant defalutVoice = sap.getProperty("Voice");
            Dispatch dispdefaultVoice = defalutVoice.toDispatch();
            Variant allVoices = Dispatch.call(sapo, "GetVoices");
            Dispatch dispVoices = allVoices.toDispatch();

            Dispatch setvoice = Dispatch.call(dispVoices, "Item", new Variant(1)).toDispatch();
            ActiveXComponent voiceActivex = new ActiveXComponent(dispdefaultVoice);
            ActiveXComponent setvoiceActivex = new ActiveXComponent(setvoice);

            Variant item = Dispatch.call(setvoiceActivex, "GetDescription");
            // 执行朗读
            Dispatch.call(sapo, "Speak", new Variant(str));

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sapo.safeRelease();
            sap.safeRelease();
        }

    }

    /*public static void synSpeakStatus(SpeakEnum speakEnum,String msg) {
        new Thread(()->{
            getInstance().speak(speakEnum.getMsg()+msg);
        },"Speak线程").start();
    }

    public static void synSpeakStatus(SpeakEnum speakEnum) {
        new Thread(()->{
            getInstance().speak(speakEnum.getMsg());
        },"Speak线程").start();
    }*/


    public static void exeSpeak(){
        //获取新的jedis连接
        Jedis jedis=JedisUtils.getNewJedis();
        new Thread(()->{
            while (true){
                if (jedis.llen(AionConstant.SPEAKLIST)>0){
                    getInstance().speak(jedis.rpop(AionConstant.SPEAKLIST));
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        },"Speak线程").start();

    }


}
